import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.List;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jruby.embed.bsf.JRubyEngine;

@WebServlet(name="Server", urlPatterns={"/Server"})
public class Server extends HttpServlet {

	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>POST</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	
	ScriptEngineManager m = new ScriptEngineManager();
	ScriptEngine rubyEngine = m.getEngineByName("jruby");
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response){//클라이언트에서 get방식으로 보낼때 응답하는 함수
		response.setContentType("text/html;charset=UTF-8");

		ServletContext ctx = request.getServletContext();
		String path = ctx.getRealPath("/");//tomcat프로젝트에서 경로 읽어올때는 이렇게

		File f;
		FileReader fr;
		try {

			f = new File(path+"test.rb");
			fr = new FileReader(f);
			rubyEngine.eval(fr);//완료 여기서 이제결과값을 가져오는 방법을 알아야함 -> rb파일 내용이 DB값을 읽어서 처리하고 DB업데이트 방식이므로 실행만하면 됨
			PrintWriter out = response.getWriter();
			out.print("asdf");
		} catch (IOException | ScriptException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response){//클라이언트에서 post방식으로 보낼때 응답하는 함수
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		response.setContentType("text/html;charset=UTF-8");
		
		ServletContext ctx = request.getServletContext();
		String path = ctx.getRealPath("/");//tomcat프로젝트에서 경로 읽어올때는 이렇게

		File f;
		FileReader fr;
		
			try {
				
				
				PrintWriter out = response.getWriter();
				f = new File(path+"test.rb");
				fr = new FileReader(f);
				rubyEngine.eval(fr);
				
				out.print("not Null");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ScriptException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			

			String numString = request.getParameter("CARD_NUMBER");
			int cardNum2x = Integer.parseInt(numString);
			for(int i=0;i<cardNum2x;i=i+2)
			{
				request.getParameter("CARD_"+i);
				request.getParameter("CARD_"+i+1);				
			}
			//userNum = userNum.substring(0, userNum.indexOf("."));
			//String t = fi.getString();

			//rubyEngine.eval("puts 1 + $label");
			

			//rubyEngine.eval(new FileReader(f));//완료 여기서 이제결과값을 가져오는 방법을 알아야함 -> rb파일 내용이 DB값을 읽어서 처리하고 DB업데이트 방식이므로 실행만하면 됨

	
		
	}
}